package com.grm.topics;

import com.grm.util.RabbitMqUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer1 {
    public static void main(String[] args) {
        getMessage();
    }

    public static void getMessage() {
        try {
            //建立连接
            Connection connection = RabbitMqUtils.getConnection();
            //建立通道
            Channel channel = connection.createChannel();
            //绑定交换机，创建队列，绑定交换机和队列
            channel.exchangeDeclare("topics", "topic");
            String queueName = channel.queueDeclare().getQueue();
            //*匹配一个单词 ，只能匹配user.save || user.delete
            String routeKey = "user.*";
            channel.queueBind(queueName, "topics", routeKey);
            channel.basicConsume(queueName, true, new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("消费者1" + new String(body));
                }
            });

            //不关闭连接跟通道

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
